package com.itheima.service.db.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.domain.db.Announcement;
import com.itheima.domain.db.UserInfo;
import com.itheima.domain.vo.PageBeanVo;
import com.itheima.mapper.AnnouncementMapper;
import com.itheima.mapper.UserInfoMapper;
import com.itheima.service.db.UserInfoService;
import com.itheima.service.db.UserService;
import org.apache.dubbo.config.annotation.Service;

import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@Service
public class UserInfoServiceImpl implements UserInfoService {

    @Autowired
    private UserInfoMapper userInfoMapper;

    @Autowired
    private AnnouncementMapper announcementMapper;

    //保存用户个人信息
    @Override
    public void save(UserInfo userInfo) {
        //基础的默认假信息数据
        userInfo.setEducation("本科");
        userInfo.setTags("单身,本科,年龄相仿");
        userInfo.setAge(18);
        userInfo.setIncome("30");
        userInfo.setMarriage(0);
        userInfo.setProfession("计算机行业");
        userInfoMapper.insert(userInfo);
    }

    //修改个人头像信息
    @Override
    public void update(UserInfo userInfo) {
        userInfoMapper.updateById(userInfo);
    }

    //根据用户的id查询用户对象
    @Override
    public UserInfo findById(Long id) {
        return userInfoMapper.selectById(id);
    }

    //分页查询用户数据
    @Override
    public PageBeanVo findUsers(Integer pageNumber, Integer pageSize) {
        //拼接分页参数
        IPage page = new Page(pageNumber, pageSize);
        //拼接分页条件
        QueryWrapper queryWrapper = new QueryWrapper();
        page = userInfoMapper.selectPage(page, queryWrapper);
        return new PageBeanVo(pageNumber, pageSize, page.getTotal(), page.getRecords());
    }

    @Override
    public PageBeanVo announcementList(Integer pageNumber, Integer pageSize) {
        //条件
        IPage<Announcement> page = new Page(pageNumber, pageSize);
        QueryWrapper<Announcement> queryWrapper = new QueryWrapper();
        //实现分页查询
        page = announcementMapper.selectPage(page, queryWrapper);
        return new PageBeanVo(pageNumber, pageSize, page.getTotal(), page.getRecords());
    }

    @Override
    public UserInfo findByNickName(String nickName) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("nickname", nickName);
        UserInfo userInfo = userInfoMapper.selectOne(queryWrapper);
        return userInfo;

    }

    @Override
    public List<UserInfo> findAll() {

        return userInfoMapper.selectList(null);
    }

    @Override
    public List<UserInfo> findByFuzzy(String nickName) {
        return null;
    }
}
